<?php
/**
 * Created by XM·Tan.
 * Motto: Who shuts love out, in turn shall be shut out from love.
 * Date: 2021/3/22 22:24
 */

namespace app\common\model\mysql;


class Admin extends BaseModel {

    protected $dateFormat = 'Y-m-d H:i:s';
    protected $type = [
        'last_login_time'  =>  'timestamp:Y-m-d H:i:s',
    ];

    /**
     * title 查询提条件表达式
     * @param $query
     * @param $value
     */
    public function searchUsernameAttr($query, $value) {
        $query->where('username', 'like', '%' . $value . '%');
    }

    /**
     * @param $query
     * @param $value
     */
    public function searchLastLoginTimeAttr($query, $value) {
        $query->whereBetWeenTime('last_login_time', $value[0], $value[1]);
    }

    /**
     * 根据用户名获取管理员数据
     * @param $username
     * @return array|bool|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAdminByUsername($username) {
        if (empty($username)) {
            return false;
        }

        $where = [
            'username' => trim($username)
        ];

        $result = $this->where($where)->find();
        return $result;
    }


}